---
title: "Group selection_graphs"
author: "Levi Orero"
date: "7/2/2020"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Group Selection Key Informants
```{r message=FALSE}
# Load libraries
library(tidyverse)
library(readr)
library(ggplot2)
library(ggthemes)
library(colorspace)
```


## Import the data
```{r import data}
group_select <- read_csv('group_select_ki.csv')

# Adapted
group_select_adapted <- read_csv('group_selection_ki_adapted.csv')
head(group_select)

# Group objective
group_objective <- read_csv("group_objective.csv")
head(group_objective)
```
## Figure 1
*Illustration of group type distribution in the two rounds of original group classification*
```{r figure1}
# graph data
round_1 <- data.frame(type = c("Type 1","Type 2","Type 3","Type 4"),
                      frequency = c(16, 0, 9, 0))
round_2 <- data.frame(type = c("Type 1","Type 2","Type 3","Type 4"),
                      frequency = c(16, 15, 9, 18))
# Set graphical parameters
par(mfrow = c(1,2))

# plot graphs
barplot(frequency ~ type, data = round_1, main = "Round 1",
        ylim = c(0,20), xlab = "Group type",
        ylab = "Group frequency")

barplot(frequency ~ type, data = round_2, main = "Round 2",
        ylim = c(0,20), xlab = "Group type",
        ylab = "Group frequency")

```

## Figure 2
*Performance assessment across various indicators per group type according to the original group type classification*

```{r figure_2}
# Create new column in dataset to show the percentage
# details (grp 1 - 7, grp 2 - 3, grp 3 - 7, grp 4 - 7)
group_select_long_perc = 
  group_select_long %>%
  mutate(percentage = ifelse(Type == "Type 1", Count*100/7, 
                              ifelse(Type == "Type 2", Count*100/3,
                                      ifelse(Type == "Type 3", Count*100/7,
                                              Count*100/7))))

# A stacked bar chart with percentages
# ggplot object
ggplot(group_select_long_perc , 
       aes(x = Group_type, y = percentage, fill = Answer)) +
  # geom_point()+
  geom_bar(stat = "identity", position = "stack") + 
  ylim(0, 100) +
  # add theme
  theme_bw() +
  # center plot title
  theme(plot.title = element_text(hjust = 0.5)) +
  # label the axes and title
  labs(x = "",
       y = "Percentage of groups (%)",
       title = "Group characteristics by type") +
  # flip the graph and wrap by group type
  coord_flip() + facet_wrap(.~Type)
```
## Figure 3
*Performance assessment across various indicators per group type according to the adapted group type classification*

```{r figure_3}
# Create a ready dataframe
group_selecty_long = 
group_select_adapted %>%
    # pivot longer
  pivot_longer(cols = starts_with("Type"),
               names_to = 'Type', 
               values_to = 'Count') %>%
  # add factored answer column
  mutate(Answer = factor(Answer, levels = c("no","yes",'very'),
                       ordered = T)) %>%
  # add factored group type column
  mutate(Group_type = factor(Group_type, 
          levels = c(
            "Final assessment about suitability as project group",
            "Performance of the group in outreach",
            "General group performance",
            "Performance of individual farmers",
            "External connectors (political/other NGOs)",
            "VSLA",
            "Exchange/Outreach",
            "Joint investments with other groups",
            "Joint activities with other groups",
            "Joint investments with other members",
            "Joint activities with other members"),
          ordered = T)) %>%
  # add percentages for plotting
  mutate(percentage = ifelse(
    Type == "Type 1", Count*100/6,
    ifelse(
      Type == "Type 2", Count*100/3,
      ifelse(Type == "Type 3", Count*100/7, Count*100/8))))

# ggplot object
ggplot(group_selecty_long , 
       aes(x = Group_type, y = percentage, fill = Answer)) +
  #geom_point()+
  geom_bar(stat = "identity", position = "stack") + 
  # add theme
  theme_bw() +
  # center title
  theme(plot.title = element_text(hjust = 0.5)) +
  # label axes and plot
  labs(x = "",
       y = "Percentage of groups (%)",
       title = "Group characteristics by adapted type") +
  # flip graph and wrap by type
  coord_flip() + facet_wrap(.~Type)

```
## Figure 4
*Performance assessment across various indicators per group objective classification*
```{r figure_4}
# Bar graph across
# order factor
group_objective$`Group characteristic` = factor(group_objective$`Group characteristic`,
                             levels = c(
                               "Joint activities with other members",
                               "Joint investments with other members",
                               "Joint activities with other groups",
                               "Joint investments with other groups",
                               "Exchange and outreach",
                               "VSLA",
                               "External connectors (political/other NGOs)",
                               "Performance of individual farmers",
                               "General group performance",
                               "Performance of the group in outreach",
                               "Final assessment about suitability as project group"),
                             ordered = T)
# prep df for plot
group_objective$groupx = str_wrap(group_objective$`Group characteristic`,
                                  width = 10)
# as factor
group_objective$groupx = factor(group_objective$groupx,
                                levels = unique(group_objective$groupx),
                                ordered = T)
# as factor for group objective
group_objective$objective = factor(group_objective$objective,
                                levels = unique(group_objective$objective),
                                ordered = T)
# plot ggplot bar graphs  
# internal social cohesion
intSC =
  ggplot(subset(group_objective,category == "Internal Social cohesion"),
       aes(x = groupx,
           y = yes_verymuch, fill = objective,
           group = objective)) +
  geom_bar(stat = "identity", position = "dodge2",
           color = "grey") + 
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.x = element_blank()) +
  labs(x = "Group characteristic",
       y = "Percentage of groups (%)",
       title = "Internal social cohesion") + scale_fill_brewer("Blues") +
  guides(fill = F) 

# external social cohesion
extSC =
  ggplot(subset(group_objective,category == "External Social cohesion"),
       aes(x = groupx,
           y = yes_verymuch, fill = objective,
           group = objective)) +
  geom_bar(stat = "identity", position = "dodge2",
           color = "grey") + 
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.x = element_blank()) +
  labs(x = "Group characteristic",
       y = "Percentage of groups (%)",
       title = "External social cohesion") + scale_fill_brewer("Blues") +
  guides(fill = guide_legend(title = "objective")) 

# Social networks
socNet =
  ggplot(subset(group_objective,category == "Social networks"),
       aes(x = groupx,
           y = yes_verymuch, fill = objective,
           group = objective)) +
  geom_bar(stat = "identity", position = "dodge2",
           color = "grey") + 
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.x = element_blank()) +
  labs(x = "Group characteristic",
       y = "Percentage of groups (%)",
       title = "Use of social networks") + scale_fill_brewer("Blues") +
  guides(fill = F) 

# Final assessment
finAse =
  ggplot(subset(group_objective,category == "Overall assessment"),
       aes(x = groupx,
           y = yes_verymuch, fill = objective,
           group = objective)) +
  geom_bar(stat = "identity", position = "dodge2",
           color = "grey") + 
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.x = element_blank()) +
  labs(x = "Group characteristic",
       y = "Percentage of groups (%)",
       title = "Overall assessment") + scale_fill_brewer("Blues") +
  guides(fill = FALSE)

# Combine graphs
library(patchwork)

combined_fig4 = ((intSC | extSC) / (socNet | finAse)) + 
  plot_annotation(title = "Group characteristics by objective",
                 theme = theme(plot.title = element_text(hjust = 0.5)))
combined_fig4  
  
```

## Figure 5
*Overall comparative performance assessment of different group types*
```{r figure_5}
# import dataset after merging "yes" and "very much
group_selecty_long_filter = read_csv("group_selecty_long_filter.csv")

  
#========================
  # Bar graph across
# order factor
group_selecty_long_filter$Group_type = factor(group_selecty_long_filter$Group_type,
                             levels = c(
                               "Joint activities with other members",
                               "Joint investments with other members",
                               "Joint activities with other groups",
                               "Joint investments with other groups",
                               "Exchange and Outreach",
                               "VSLA",
                               "External connectors (political/other NGOs)",
                               "Performance of individual farmers",
                               "General group performance",
                               "Performance of the group in outreach",
                               "Final assessment about suitability as project group"),
                             ordered = T)
# prep df for plot
group_selecty_long_filter$groupx = str_wrap(group_selecty_long_filter$Group_type ,
                                  width = 10)
# as factor
group_selecty_long_filter$groupx  = factor(group_selecty_long_filter$groupx ,
                                levels = unique(group_selecty_long_filter$groupx),
                                ordered = T)
# as factor for group type
group_selecty_long_filter$Type = factor(group_selecty_long_filter$Type,
                                levels = unique(group_selecty_long_filter$Type),
                                ordered = T)


# plot ggplot bar graph  
# internal social cohesion
intSC_ada =
  ggplot(subset(group_selecty_long_filter,category == "Internal social cohesion"),
       aes(x = groupx,
           y = percentage, fill = Type,
           group = Type)) +
  geom_bar(stat = "identity", position = "dodge2",
           color = "grey") + 
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.x = element_blank()) +
  labs(x = "Group characteristic",
       y = "Percentage of groups (%)",
       title = "Internal social cohesion") + scale_fill_brewer("Blues") +
  guides(fill = F) 

# external social cohesion
extSC_ada =
  ggplot(subset(group_selecty_long_filter,category == "External social cohesion"),
       aes(x = groupx,
           y = percentage, fill = Type,
           group = Type)) +
  geom_bar(stat = "identity", position = "dodge2",
           color = "grey") + 
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.x = element_blank()) +
  labs(x = "Group characteristic",
       y = "Percentage of groups (%)",
       title = "External social cohesion") + scale_fill_brewer("Blues") +
  guides(fill = guide_legend(title = "objective")) 

# Social networks
socNet_ada =
  ggplot(subset(group_selecty_long_filter,category == "Social networks"),
       aes(x = groupx,
           y = percentage, fill = Type,
           group = Type)) +
  geom_bar(stat = "identity", position = "dodge2",
           color = "grey") + 
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.x = element_blank()) +
  labs(x = "Group characteristic",
       y = "Percentage of groups (%)",
       title = "Use of social networks") + scale_fill_brewer("Blues") +
  guides(fill = F) 

# Final assessment
finAse_ada =
  ggplot(subset(group_selecty_long_filter,category == "Overall assessment"),
       aes(x = groupx,
           y = percentage, fill = Type,
           group = Type)) +
  geom_bar(stat = "identity", position = "dodge2",
           color = "grey") + 
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.x = element_blank()) +
  labs(x = "Group characteristic",
       y = "Percentage of groups (%)",
       title = "Overall assessment") + scale_fill_brewer("Blues") +
  guides(fill = FALSE)

# Combine graphs
library(patchwork)

combined_fig5 = ((intSC_ada | extSC_ada) / (socNet_ada | finAse_ada)) + 
  plot_annotation(title = "Group characteristics by adapted type",
                 theme = theme(plot.title = element_text(hjust = 0.5)))
combined_fig5  
```






